#!/usr/bin/env python
# encoding: utf-8
"""
#-------------------------------------------------------------------#
#                   CONFIDENTIAL --- CUSTOM STUDIOS                 #     
#-------------------------------------------------------------------#
#                                                                   #
#                   @Project Name : Globallawonline                #
#                                                                   #
#                   @File Name    : cutmdcasepdf.py                      #
#                                                                   #
#                   @Programmer   : 李建                            #
#                                                                   #  
#                   @Start Date   : 2021/4/15 9:14                 #
#                                                                   #
#                   @Last Update  : 2021/4/15 9:14                 #
#                                                                   #
#-------------------------------------------------------------------#
# Classes:用于处理pdf文件的类                                                         #
#                                                                   #
#-------------------------------------------------------------------#
"""
# import PyPDF2
import os
import re


class PdfFunction:
    """
    该类用于处理pdf文件
    """
    def __init__(self):
        pass

    def readpdf(self,pdf_path):
        """
        pdf文件读取
        :param pdf_path:pdf文件路径
        :return:
        """
        import PyPDF2
        input_pdf = PyPDF2.PdfFileReader(open(pdf_path,mode="rb"))
        dagang = input_pdf.getOutlines()    # 获取pdf大纲
        page_number = input_pdf.getNumPages()   # 获取pdf页数
        page_layout = input_pdf.getPageLayout()
        page_model = input_pdf.getPageMode()
        page_str = input_pdf.getPage(30).extractText()
        no_list = []
        for i in range(28,page_number-1):
            txt = input_pdf.getPage(i).extractText()
            if not re.search("BURMA LAW REPORTS", txt):
                no_list.append(i)
        print(no_list)
        page_pdf = input_pdf.getPage(30) # 获取pdf对应页面
        txt = page_pdf.extractText()    # 获取当前页面的文本内容
        pass

    def getpdfnumber(self, pdf_path):
        """
        获取pdf文件的页数
        :param pdf_path:pdf文件路径
        :return:
        """
        import PyPDF2
        input_pdf = PyPDF2.PdfFileReader(open(pdf_path, mode="rb"))
        page_number = input_pdf.getNumPages()  # 获取pdf页数
        return page_number

    def pdf_splitter(self, pdf_path,start,end,out_path):
        """
        按照页码进行pdf文件分割
        :param pdf_path:pdf文件路径
        :param start:文件分割开始页页码
        :param end:文件分割结束页页码
        :param out_dir:分割的pdf文件存放路径
        :return:
        """
        import PyPDF2
        pdf = PyPDF2.PdfFileReader(pdf_path)
        pdf_writer = PyPDF2.PdfFileWriter()
        output_filename = os.path.split(out_path)[1]
        try:
            for page in range(start-1, end):
                pdf_writer.addPage(pdf.getPage(page))
        except Exception as e:
            print("%d-%d页码超出%spdf范围:%s" % (start,end,pdf_path,str(e)))
            return 1
        out_dir = os.path.split(out_path)[0]
        if not os.path.exists(out_dir):
            os.makedirs(out_dir)
        with open(out_path,'wb') as out:
            pdf_writer.write(out)
            print('Created:{}'.format(output_filename))
        return out_path


if __name__ == '__main__':
    # pdf_path = os.path.join("E:\工作记录\工作内容\“一带一路”项目\各国专家提供的法律资源资料\老挝\PDF\legislation\\29. C14_41.pdf")
    # result = PdfFunction().readpdf(pdf_path)
    pdf_root = os.path.join(r"C:\Users\LJ13284\Desktop\erro")
    list = os.listdir(pdf_root)  # 列出文件夹下所有的目录与文件
    for i in range(0, len(list)):
        path = os.path.join(pdf_root, list[i])
        path = r"C:\Users\LJ13284\Desktop\erro\ff3c2400ab5fa4b1f962ae108ed530c17.pdf"
        if path.__contains__(".pdf"):
            PdfFunction().readpdf(path)
            page = PdfFunction().getpdfnumber(path)
            with open("page.txt", "a", encoding='utf-8') as f:
                f.write(path + ";" + str(page) + '\n')

